package com.flying.invest.account.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.service.AbstractService;
import com.flying.invest.account.model.AccountNetValue;

@Service(value="AccountNetValueService", desc="账户净值")
public class AccountNetValueService extends AbstractService{
        
	@MethodInfo("新增")
	@DaoCreate(entity=AccountNetValue.ENTITY_NAME)
	public AccountNetValue create(
			@Param(value=AccountNetValue.USER_ID, required=true,	desc="所属用户") Long user_id,
			@Param(value=AccountNetValue.ACCOUNT_ID, required=true,	desc="账户ID") Long account_id,
			@Param(value=AccountNetValue.ACCOUNT_DATE, required=true,	desc="记账日期") String account_date,
			@Param(value=AccountNetValue.NET_VALUE, required=false,	desc="净值") Double net_value,
			@Param(value=AccountNetValue.SHARES, required=false,	desc="份额") Double shares,
			@Param(value=AccountNetValue.TOTAL_AMOUNT, required=false,	desc="总额") Double total_amount,
			@Param(value=AccountNetValue.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		) {
		
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=AccountNetValue.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=AccountNetValue.ACCOUNT_ID, required=false,	desc="账户ID") Long account_id,
			@Param(value=AccountNetValue.ACCOUNT_DATE, required=false,	desc="记账日期") String account_date
		) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@MethodInfo("搜索")
	@DaoQuery(entity=AccountNetValue.ENTITY_NAME, pageable=true, modelClass=AccountNetValue.class, 
			qsql="select * from(\r\n"
					+ "select t.user_id ,t.account_id ,t.account_date , sum(t.total_amount) / sum(t.shares ) as net_value, sum(t.shares) as shares , sum(t.total_amount) as total_amount \r\n"
					+ "from i_account_net_value t \r\n"
					+ "group by t.user_id ,t.account_id ,t.account_date\r\n"
					+ ") t")
	public QueryResult<AccountNetValue> findByUserId (
			@Param(value=AccountNetValue.USER_ID, required=false,	desc="所属用户") Long user_id,
			@Param(value=AccountNetValue.ACCOUNT_DATE, required=true,	desc="记账日期", tag="BETWEEN") String[] account_date,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		var p = ServiceContext.getContext().getPrincipal();
		if(user_id == null) {
			user_id = Long.parseLong(p.getId());
		}
		return ModelFactory.create(QueryResult.class, AccountNetValue.USER_ID, user_id);
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=AccountNetValue.ENTITY_NAME, pageable=true, modelClass=AccountNetValue.class)
	public QueryResult<AccountNetValue> findByAccountId (
			@Param(value=AccountNetValue.ACCOUNT_ID, required=true,	desc="账户ID") Long account_id,
			@Param(value=AccountNetValue.ACCOUNT_DATE, required=true,	desc="记账日期", tag="LIKE") String[] account_date,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=AccountNetValue.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public AccountNetValue findById(
			@Param(value=AccountNetValue.NET_VALUE_ID, required=true,	desc="ID") Long net_value_id
		) {
		return null;
	}

}